package com.cinc.ebc.fi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cinc.ebc.fi.constant.AparInitialStatus;
import com.cinc.ebc.fi.entity.FiApOriginalAmount;
import com.cinc.ebc.fi.entity.FiArOriginalAmount;
import com.cinc.ebc.fi.mapper.FiArOriginalAmountMapper;
import com.cinc.ebc.fi.model.FiArOriginalAmountModel;
import com.cinc.ebc.fi.service.IFiArOriginalAmountService;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 应收初始化
 * @Author: jeecg-boot
 * @Date:   2022-12-07
 * @Version: V1.0
 */
@Service
public class FiArOriginalAmountServiceImpl extends ServiceImpl<FiArOriginalAmountMapper, FiArOriginalAmount> implements IFiArOriginalAmountService {

    @Autowired
    private FiArOriginalAmountMapper fiArOriginalAmountMapper;

    @Override
    public void pageList(Page<FiArOriginalAmountModel> page, FiArOriginalAmount fiArOriginalAmount) {
        String sql = QueryGenerator.installAuthJdbc(FiApOriginalAmount.class);
        page.setRecords(fiArOriginalAmountMapper.selectModelList(page, fiArOriginalAmount, sql));
    }

    @Transactional
    @Override
    public void handleInitial(List<FiArOriginalAmount> fiArOriginalAmounList) {
        for(FiArOriginalAmount fiArOriginalAmount : fiArOriginalAmounList){
            FiArOriginalAmount his = fiArOriginalAmountMapper.selectById(fiArOriginalAmount.getId());
            if(!AparInitialStatus.STATUS_10.equals(his.getStatus())){
                throw new RuntimeException("已初始化单据，不能再次初始化");
            }
            BigDecimal initialBalance = his.getInitialBalance();
            BigDecimal calculate = his.getRecAmount().add(his.getOtherAmount()).add(his.getInitialRevenue()).subtract(his.getAdvanceRecAmount());
            if(calculate.compareTo(BigDecimal.ZERO) != 0 && initialBalance.compareTo(calculate) != 0){
                throw new RuntimeException("期初余额计算有误，请先修改");
            }
            FiArOriginalAmount fiArOriginalAmountMoidfy = new FiArOriginalAmount();
            fiArOriginalAmountMoidfy.setId(fiArOriginalAmount.getId());
            fiArOriginalAmountMoidfy.setStatus(AparInitialStatus.STATUS_20);
            fiArOriginalAmountMapper.updateById(fiArOriginalAmountMoidfy);
        }
    }

    @Override
    public boolean save(FiArOriginalAmount fiArOriginalAmount) {
        LambdaQueryWrapper<FiArOriginalAmount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FiArOriginalAmount::getCustomerId, fiArOriginalAmount.getCustomerId());
        int count = fiArOriginalAmountMapper.selectCount(queryWrapper);
        if(count > 0){
            throw new RuntimeException("此客户已存在记录");
        }
        return super.save(fiArOriginalAmount);
    }

    @Override
    public boolean updateById(FiArOriginalAmount fiArOriginalAmount) {
        LambdaQueryWrapper<FiArOriginalAmount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FiArOriginalAmount::getCustomerId, fiArOriginalAmount.getCustomerId());
        queryWrapper.ne(FiArOriginalAmount::getId, fiArOriginalAmount.getId());
        int count = fiArOriginalAmountMapper.selectCount(queryWrapper);
        if(count > 0){
            throw new RuntimeException("此客户已存在记录");
        }
        return super.updateById(fiArOriginalAmount);
    }
}
